﻿using System;
using System.Collections.Generic;
using System.Text;
using xReplace;
using System.IO;

namespace AssemblyInfoPlugin
{
    class AssemblyInfoCommand : AbstractProcessingCommand
    {
        public AssemblyInfoCommand(ref Content pipes)
            : base(ref pipes)
        { }

        public override bool ProcessCommand(System.Xml.XmlNodeList param)
        {
            string assemblyFile = this.GetNodeValue("assemblyfilename", param);
            string delimiter=this.GetNodeValue("delimiter", param);
            string toLabel = this.GetNodeValue("tolabel", param);
            string renameRules = this.GetNodeValue("renamerules", param);
            if (renameRules != "")
                assemblyFile = RenameHelper.PerformRename(assemblyFile, renameRules, this.contentPipelines);

            if (!File.Exists(assemblyFile)) throw new Exception("File not found: " + assemblyFile);
            AsmInfoVersion ai = new AsmInfoVersion(assemblyFile);
            if (delimiter == "") delimiter = ".";
            if (toLabel != "")
            {
                this.contentPipelines.StoreContent(toLabel, ai.GetVersion(delimiter));
            }
            else
            {
                this.contentPipelines.DefaultContent = ai.GetVersion(delimiter);
            }
            return true;
        }
    }
}
